package cn.wolfcode.p2p.mgrsite.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.p2p.base.domain.SystemDictionary;
import cn.wolfcode.p2p.base.domain.SystemDictionaryItem;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.query.SystemDictionaryQueryObject;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;
import cn.wolfcode.p2p.base.util.JSONResult;

/**
 * 数据字典相关控制器
 * 
 * @author wolfcode
 *
 */
@Controller
public class SystemDictionaryController {

	@Autowired
	private ISystemDictionaryService dicService;

	/**
	 * 数据字典目录列表
	 */
	@RequestMapping("systemDictionary_list")
	public String dicList(@ModelAttribute("qo") SystemDictionaryQueryObject qo, Model model) {
		model.addAttribute("pageResult", this.dicService.queryDics(qo));
		return "systemdic/systemDictionary_list";
	}

	/**
	 * 数据字典明细列表
	 */
	@RequestMapping("systemDictionaryItem_list")
	public String itemList(@ModelAttribute("qo") SystemDictionaryQueryObject qo, Model model) {
		model.addAttribute("pageResult",this.dicService.queryItems(qo));
		model.addAttribute("systemDictionaryGroups",this.dicService.listAllDics());
		return "systemdic/systemDictionaryItem_list";
	}

	/**
	 * 修改或者保存数据字典目录
	 * 
	 */
	@RequestMapping("systemDictionary_update")
	@ResponseBody
	public JSONResult saveOrUpdateDic(SystemDictionary dic) {
		JSONResult result = new JSONResult();
		try {
			this.dicService.saveOrUpdate(dic);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 修改或者添加明细
	 */
	@RequestMapping("systemDictionaryItem_update")
	@ResponseBody
	public JSONResult saveOrUpdateItem(SystemDictionaryItem item) {
		JSONResult result = new JSONResult();
		try {
			this.dicService.saveOrUpdateItem(item);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}

}
